package com.hbxy.favorite.service.impl;

import com.hbxy.favorite.dao.BackUserMapper;
import com.hbxy.favorite.dao.FollowsMapper;
import com.hbxy.favorite.dao.UserMapper;
import com.hbxy.favorite.domain.*;
import com.hbxy.favorite.resault.ResponseBo;
import com.hbxy.favorite.service.UserService;
import com.hbxy.favorite.utils.MD5Utils;
import com.hbxy.favorite.utils.RegUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author zhongda
 * @Date 2019/3/18 15:39
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private  FollowsMapper followsMapper;

    @Autowired
    private BackUserMapper backUserMapper;

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Override
    public User login(User user) {

        return userMapper.login(user.getUserName(),user.getPassWord());
    }

    @Override
    public ResponseBo register(User user) {
        Favorites favorites =new Favorites();
        if(RegUtil.isNull(user.getUserName())||RegUtil.isNull(user.getEmail()))
            return ResponseBo.error("邮箱或用户名不能为空");
        if(RegUtil.isNull(user.getPassWord()))
            return ResponseBo.error("请输入密码");
        if((user.getEmail()!=null && user.getUserName()!= null) && user.getPassWord()!=null){
            User user1 = userMapper.findOne(user.getUserName());
            User user2 = userMapper.findByEmail(user.getEmail());
            if(user1 !=null){
                return ResponseBo.error("用户名已被注册");
            }
            if(user2 != null){
                return ResponseBo.error("该邮箱已被注册");
            }

            favorites.setUserId(user.getId());
            user.setCreateTime(new Date().getTime());
            user.setLastModifyTime(new Date().getTime());
            user.setPassWord(MD5Utils.encrypt(user.getPassWord()));
            userMapper.insert(user);
            return ResponseBo.ok("注册成功");
        }else
        return ResponseBo.error("请输入必要信息");
    }

    @Override
    public User findByUsername(String username) {
        return userMapper.findOne(username);
    }

    @Override
    public User findByEmail(String email) {
        return userMapper.findByEmail(email);
    }

    @Override
    public ResponseBo getUserMessage(User user) {
        if(user!=null && user.getId()!=null){
            user = userMapper.selectByPrimaryKey(user.getId());
        }
        return ResponseBo.ok(user);
    }

    @Override
    public User getMyPersional(Long id) {
        if(id!=null){
            User user =userMapper.selectByPrimaryKey(id);
            return user;
        }
        return null;
    }
    @Override
    public User getOnePersional(Long id) {
        if(id!=null){
            User user =userMapper.selectByPrimaryKey(id);
            return user;
        }
        return null;
    }

    @Override
    public ResponseBo addFollow(Follows follows) {
        if(RegUtil.isNotNull(follows.getFansId())&& RegUtil.isNotNull(follows.getFollowId())){
            FollowsExample followsExample =new FollowsExample();
            FollowsExample.Criteria criteria= followsExample.createCriteria();
            criteria.andFansIdEqualTo(follows.getFansId());
            List<Follows> followsList =  followsMapper.selectByExample(followsExample);
            for(Follows f:followsList ){
                if(f.getFollowId()==follows.getFollowId()){

                    return ResponseBo.ok("已关注");
                }
            }
            followsMapper.insert(follows);
            return  ResponseBo.ok("已关注");


        }
        return ResponseBo.error("关注失败，缺少参数！");
    }

    @Override
    public List<User> getFollows(Long userId) {

        List<User> userList =new ArrayList<>();
        if(userId !=null){
            User user;
            FollowsExample followsExample =new FollowsExample();
            FollowsExample.Criteria criteria =followsExample.createCriteria();
            criteria.andFansIdEqualTo(userId);

            List<Follows> followsList = followsMapper.selectByExample(followsExample);
            for(Follows follows : followsList){
                user= userMapper.selectByPrimaryKey(follows.getFollowId());
                userList.add(user);
            }
            return userList;
        }
        return null;

    }


    @Override
    public ResponseBo qxFollow(Follows follows) {
        if(RegUtil.isNotNull(follows.getFansId())){
            FollowsExample followsExample =new FollowsExample();
            FollowsExample.Criteria criteria =followsExample.createCriteria();
            criteria.andFansIdEqualTo(follows.getFansId());
            List<Follows> followsList = followsMapper.selectByExample(followsExample);

            for(Follows follows1 : followsList){
                if(follows.getFollowId().equals(follows1.getFollowId())){
                    followsMapper.deleteByPrimaryKey(follows1.getId());
                    return ResponseBo.ok("取消成功！");
                }
            }
        }
        return ResponseBo.error("缺少参数！");


    }

    @Override
    public ResponseBo updateUser(User user) {

       if(RegUtil.isNull(user.getUserName()) || RegUtil.isNull(user.getEmail())){
           return ResponseBo.error("用户名或邮箱不能为空");
       }
       /* User user3 = userMapper.findOne(user.getUserName());
        User user2 = userMapper.findByEmail(user.getEmail());
        if(user3 !=null){
            return ResponseBo.error("用户名已存在");
        }
        if(user2 != null){
            return ResponseBo.error("邮箱已存在");
        }*/
        if(RegUtil.isNotNull(user.getId())){
            User user1=userMapper.selectByPrimaryKey(user.getId());
            user1.setUserName(user.getUserName());
            user1.setEmail(user.getEmail());
            userMapper.updateByPrimaryKey(user1);
            return ResponseBo.ok("修改成功");
        }

        return ResponseBo.error("缺少参数");
    }

    @Override
    public ResponseBo updatePassword(User user) {

        if(RegUtil.isNull(user.getEmail())){
            return ResponseBo.error("请确认密码");
        }
        if(RegUtil.isNull(user.getPassWord())){
            return ResponseBo.error("请输入密码");
        }
        if(!user.getPassWord().equals(user.getEmail())){
            return ResponseBo.error("两次输入密码不一致");
        }
        String password = MD5Utils.encrypt(user.getPassWord());

        if(RegUtil.isNotNull(user.getId())){
            User user1=userMapper.selectByPrimaryKey(user.getId());
            user1.setPassWord(password);
            userMapper.updateByPrimaryKey(user1);
            return ResponseBo.ok("修改成功");
        }
        return ResponseBo.error("参数错误");
    }


    @Override
    public List<User> getUserSearch(String userName) {

        UserExample example =new UserExample();
        List<User> userList;

        userList =userMapper.selectByExample(example);
        if(userName !=null){
            User user;
         UserExample userExample =new UserExample();
         UserExample.Criteria criteria =userExample.createCriteria();
         criteria.andUserNameLike("%"+userName+"%");

          userList= userMapper.selectByExample(userExample);
            return userList;
        }

        return userList;
    }



    @Override
    public List<User> getUsers(Long id) {
        List<User> userList =new ArrayList<>();
        if(id !=null){
            User user;
            FollowsExample followsExample =new FollowsExample();
            FollowsExample.Criteria criteria =followsExample.createCriteria();
            criteria.andFollowIdEqualTo(id);
            List<Follows> followsList = followsMapper.selectByExample(followsExample);
            for(Follows follows : followsList){
                user= userMapper.selectByPrimaryKey(follows.getFansId());
                userList.add(user);
            }
            return userList;
        }
        return null;
    }
}
